<template>
  <div class="content">
    <el-card class="bottom10">
      <el-collapse v-model="activeNames">
        <el-collapse-item name="1">
          <template slot="title">
            <span style="font-size:18px;">筛选查询</span>
          </template>
          <el-form ref="form" :model="form" label-width="100px">
            <el-row :gutter="20" type="flex" justify="">
              <el-col :span="6">
                <el-form-item label="输入搜索：">
                  <el-input v-model="form.name" placeholder="请输入关键词查询" />
                </el-form-item>
              </el-col>
              <el-col :span="5">
                <el-form-item label="角色类型：">
                  <el-select v-model="form.role" placeholder="请选择" clearable>
                    <el-option v-for="item in $roleOption" :key="item.value" :label="item.label" :value="item.value" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="2">
                <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
              </el-col>
            </el-row>
          </el-form>
        </el-collapse-item>
      </el-collapse>
      <el-row class="flex-hs tabTop">
        <el-col :span="24">
          <div class="flex-hs">
            <el-button type="success" @click="addItem()">添加角色</el-button>
            <el-button type="danger" :disabled="multipleSelection.length===0" @click="deleteItem(1)">删除</el-button>
          </div>
        </el-col>
      </el-row>
      <el-row class="flex-hs">
        <el-table :data="tableData" border style="width: 100%;margin:20px 0" @selection-change="handleSelectionChange">
          <el-table-column type="selection" label="" width="50" al />
          <el-table-column type="index" label="序号" width="50" />
          <el-table-column prop="id" label="编号" width="150" />
          <el-table-column prop="name" label="角色名称" />
          <el-table-column prop="role" label="角色类型" width="200">
            <template slot-scope="scope">
              <span>{{ $role[scope.row.role] }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="updatedate" label="更新时间" width="200" />
          <el-table-column prop="state" label="状态" width="150">
            <template slot-scope="scope">
              <el-tag :type="{1:'primary',2:'danger',3:'info',4:'warning'}[scope.row.state]">{{$state[scope.row.state]}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="60">
            <template slot-scope="scope">
              <el-dropdown @command="handleCommand">
                <span class="el-dropdown-link">
                  <i class="el-icon-more-outline" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item :command="whatIclick('look',scope.row)">查看</el-dropdown-item>
                  <el-dropdown-item :command="whatIclick('update',scope.row)">修改</el-dropdown-item>
                  <el-dropdown-item :command="whatIclick('delete',scope.row)">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </template>

          </el-table-column>
        </el-table>
      </el-row>
      <el-row class="flex-he">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-row>
    </el-card>
    <add-or-upload v-if="openAddOrUpload" ref="addOrUpload" @save="search" />
    <look-info v-if="openLookInfo" ref="lookInfo" />
  </div>
</template>

<script>

import addOrUpload from './addORupload'
import lookInfo from './lookInfo'
import { mapState } from 'vuex'
export default {
  components: {
    addOrUpload,
    lookInfo
  },
  data() {
    return {
      activeNames: ['1'],
      currentPage: 1,
      pageSize: 20,
      total: 100,
      form: {
        name: '',
        role: ''
      },
      openAddOrUpload: false,
      openLookInfo: false,
      multipleSelection: [],
      tableData: []
    }
  },
  computed: {
    ...mapState(['$roleOption','$role','$state']),
    queryData() {
      const obj = this.form
      obj.count = this.pageSize,
      obj.page = this.currentPage
      return obj
    }
  },
  mounted() {
    this.search()
  },
  methods: {
    search() {
      this.$http.httpPost('/role/roleList', this.queryData).then(res=>{
        if (res.data.code == 0) {
          this.total = res.data.data.total
          this.tableData = res.data.data.data
        }
      })
    },
    addItem(val) {
      let rowdata = val?JSON.parse(JSON.stringify(val)):''
      this.openAddOrUpload = true
      this.$nextTick(() => {
        this.$refs.addOrUpload.init(rowdata)
      })
    },
    whatIclick(type, data) {
      return {
        type,
        data
      }
    },
    deleteItem(val) {
      const idarr = []
      if (val === 1) {
        this.multipleSelection.forEach(e => {
          idarr.push(e.id)
        })
      } else {
        idarr.push(val.id)
      }
      const ids = idarr.join()
      this.$confirm('此操作将删除角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
         let res = this.$http.httpPost('/role/delRole', { ids: ids })
          if (res) {
            this.$operateTip('已完成！', '已完成数据删除！', 'success')
          }
          this.search()
        
      }).catch(() => {
        this.$operateTip('已取消！', '已取消数据删除！', 'info')
      })
    },
    handleCommand(e) {
      if (e.type === 'update') {
        this.addItem(e.data)
      } else if (e.type === 'delete') {
        this.deleteItem(e.data)
      } else if (e.type === 'look') {
        this.openLookInfo = true
        this.$nextTick(() => {
          this.$refs.lookInfo.init(e.data)
        })
      }
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.currentPage = 1
      this.search()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.search()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    }
  }
}
</script>
<style lang="less">

</style>
